﻿using GroupThreeMESProject.DTO;
using GroupThreeMESProject.Entity;
using GroupThreeMESProject.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace GroupThreeMESProject.Service.WarehouseService
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class WarehouseManagement : ApplicationService
    {
        private readonly ISqlSugarClient _db;

        public WarehouseManagement(ISqlSugarClient db)
        {
            _db = db;
        }
        /// <summary>
        /// 库区类型列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetWarehousesType")]
        public async Task<ApiResult<List<warehouseType>>> GetWarehousesType()
        {

            var list = await _db.Queryable<warehouseType>().ToListAsync();
            return ApiResult<List<warehouseType>>.Success(list);
        }
        /// <summary>
        /// 堆放类型列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetstackingType")]
        public async Task<ApiResult<List<stackingType>>> GetstackingType()
        {
            var list = await _db.Queryable<stackingType>().ToListAsync();
            return ApiResult<List<stackingType>>.Success(list);
        }
        /// <summary>
        /// 库区列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("GetWarehouses")]
        public async Task<ApiResult<List<WarehouseListDTO>>> GetWarehouses()
        {
            
            var list = await _db.Queryable<MesWarehouse, warehouseType, stackingType>((a, b, c) =>
                a.warehouseTypeId == b.warehouseTypeId && a.stackingTypeId == c.stackingTypeId).Select((a, b, c) =>
                 new WarehouseListDTO
                 {
                     id = a.id,
                     warehouseTypeId = b.warehouseTypeId,
                     warehouseTypeName = b.warehouseTypeName,
                     stackingTypeId = c.stackingTypeId,
                     stackingTypeName =c.stackingTypeName,
                     warehouseNumber = a.warehouseNumber,
                     warehousePprefixDigits = a.warehousePprefixDigits,
                     validIdentification = a.validIdentification,
                     firstInfirstout = a.firstInfirstout,
                     remarks = a.remarks,
                     createTime = a.createTime,
                     deleteIs = a.deleteIs,
                     status = a.status
                 }).ToListAsync();
            return ApiResult<List<WarehouseListDTO>>.Success(list);
        }
       
        /// <summary>
        /// 添加库区
        /// </summary>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        [HttpPost("AddWarehouse")]
        public async Task<ApiResult<string>> AddWarehouse(MesWarehouse warehouse)
        {
            var result = await _db.Insertable(warehouse).ExecuteCommandAsync();
            if (result == 0) return ApiResult<string>.Error("添加失败");
            return ApiResult<string>.Success("添加成功");
        }

        /// <summary>
        /// 修改库区
        /// </summary>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        [HttpPost("UpdateWarehouse")]
        public async Task<ApiResult<string>> UpdateWarehouse(MesWarehouse warehouse)
        {
            var result = await _db.Updateable(warehouse).ExecuteCommandAsync();
            if (result == 0) return ApiResult<string>.Error("修改失败");
            return ApiResult<string>.Success("修改成功");
        }

        /// <summary>
        /// 逻辑删除库区
        /// </summary>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        [HttpPost("DeleteWarehouse")]
        public async Task<ApiResult<string>> DeleteWarehouse(MesWarehouse warehouse)
        {
            var result = await _db.Deleteable(warehouse).ExecuteCommandAsync();
            if (result == 0) return ApiResult<string>.Error("删除失败");
            return ApiResult<string>.Success("删除成功");
        }
        //[HttpPost("DeleteWarehouse")]
        //public async Task<ApiResult<bool>> DeleteWarehouse(int id)
        //{
        //    var entity = await _db.Queryable<MesWarehouse>()
        //       .FirstAsync(x => x.id == id);

        //    if (entity == null)
        //    {
        //        return ApiResult<bool>.Error("未找到对应主键的仓库记录");
        //    }

        //    entity.deleteIs = true;

        //    var result = await _db.Updateable(entity)
        //       .ExecuteCommandAsync();

        //    if (result > 0)
        //    {
        //        return ApiResult<bool>.Success(true);
        //    }
        //    else
        //    {
        //        return ApiResult<bool>.Error("删除仓库记录失败");
        //    }
        //}





    }
}
